package com.pap.product.product.agent.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.tamper.BeanCopy;
import com.alibaba.tamper.process.convertor.Convertor;
import com.alibaba.tamper.process.convertor.ConvertorHelper;
import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.business.StatusFlagEnum;
import com.pap.product.agent.product.IProductAgent;
import com.pap.product.agent.product.dto.ProductDTO;
import com.pap.product.agent.product.dto.ProductDetailDTO;
import com.pap.product.product.auto.entity.Product;
import com.pap.product.product.auto.entity.ProductDetail;
import com.pap.product.product.auto.mapper.ProductDetailMapper;

@Transactional
@Service("productAgent")
public class ProductAgentImpl extends PapBaseServiceImpl<Product> implements IProductAgent {

	@Autowired
	private ProductDetailMapper productDetailMapper;
	
	@Override
	public int saveOrUpdateProductWithDetail(ProductDTO inputProductDTO, String clientLicenseId, String loginUserId) {
		String inputProductId = inputProductDTO.getId();
		String currentOperProductId = inputProductId;
		if(StringUtilss.isNotEmpty(currentOperProductId)) {
			productDetailMapper.deleteByProductId(currentOperProductId);
		} else {
			currentOperProductId = UUID.randomUUID().toString();
		}
		
		// BeanCopy
		Product databaseProduct = new Product();
		BeanCopy.create(ProductDTO.class, Product.class).copy(inputProductDTO, databaseProduct);
		databaseProduct.setStatusFlag(StatusFlagEnum.YES.getKey());
		
		List<ProductDetail> databaseProductDetailList = new ArrayList<ProductDetail>();
		List<ProductDetailDTO> inputDetailList = inputProductDTO.getDetails();
		if(inputDetailList != null) {
			ConvertorHelper helper = new ConvertorHelper();
			Convertor intSet = helper.getConvertor(List.class, Set.class);
			databaseProductDetailList = (List) intSet.convertCollection(inputDetailList, List.class, ProductDetail.class);
		}
		
		if(StringUtilss.isNotEmpty(inputProductId)) {
			mapper.updateByPrimaryKeySelective(databaseProduct);
		} else {
			databaseProduct.setId(currentOperProductId);
			databaseProduct.setCreateIp("0.0.0.0");
			databaseProduct.setCreateUser(loginUserId);
			databaseProduct.setCreateTime(DateUtils.getCurrDateTimeStr());
			databaseProduct.setClientLicenseId(clientLicenseId);
			mapper.insertSelective(databaseProduct);
		}
		
		if(databaseProductDetailList != null) {
			for (ProductDetail detailDTO : databaseProductDetailList) {
				detailDTO.setId(UUID.randomUUID().toString());
				detailDTO.setProductId(currentOperProductId);
				detailDTO.setCreateIp("0.0.0.0");
				detailDTO.setCreateUser(loginUserId);
				detailDTO.setCreateTime(DateUtils.getCurrDateTimeStr());
				detailDTO.setClientLicenseId(clientLicenseId);
				productDetailMapper.insertSelective(detailDTO);
			}
		}
		return 1;
	}

	@Override
	public int deleteProductWithDetail(String productId) {
		mapper.deleteByPrimaryKey(productId);
		productDetailMapper.deleteByProductId(productId);
		return 1;
	}

}
